
#include "mainwindow.h"
#include "MavenPackageWidget.h"
#include "MavenResult.h"
#include "MavenSearch.h"

#include <QGridLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QComboBox>
#include <QPushButton>
#include <QPushButton>
#include <QListWidget>
#include <qboxlayout.h>
#include <QGroupBox>
#include <qlineedit.h>



MainWindow::MainWindow(QWidget *parent) : QWidget(parent)
{
    MavenSearch *search = new MavenSearch;
    MavenPackageWidget *packageWidget = new MavenPackageWidget;

    QLabel *keyLabel = new QLabel("关键词");
    QLineEdit *searchEdit = new QLineEdit();
    searchEdit->setPlaceholderText("g:junit,a:junit");
    QPushButton *searchButton = new QPushButton("搜索");
    QLabel *searchNumberLabel = new QLabel("条目数:");
    QLineEdit *searchNumber = new QLineEdit("100");

    QIntValidator *validator = new QIntValidator();
    validator->setRange(5, 100);
    searchNumber->setValidator(validator);

    QListWidget *resultWidget = new QListWidget;

    connect(resultWidget, &QListWidget::currentTextChanged,[=](const QString &currentText){
        MavenResult *result = search->getResult(currentText);
        if (result) {
            packageWidget->setPackage(result);
        }
    });

    connect(search, &MavenSearch::doc, [=](MavenResult *doc){
        resultWidget->addItem(QString("%1:%2").arg(doc->id).arg(doc->latestVersion));
    });

    connect(search, &MavenSearch::docs, [=](QList<MavenResult*> docs){
        resultWidget->clear();
        for (auto &doc : docs) {
            int cnt = doc->id.split(":").count();
            if (doc->id.split(":").count() == 2)
                resultWidget->addItem(QString("%1:%2").arg(doc->id).arg(doc->latestVersion));
            else
                resultWidget->addItem(QString("%1").arg(doc->id));
        }
    });

    connect(searchEdit, &QLineEdit::textChanged, [=](const QString &text){
        // resultWidget->clear();
        search->search(searchEdit->text(), searchNumber->text().toInt(), "json");
    });
    connect(searchButton, &QPushButton::clicked, [=]{
        // resultWidget->clear();
        search->search(searchEdit->text(), searchNumber->text().toInt(), "json");
    });

// 响应Maven包信息展示控件事件
    connect(packageWidget, &MavenPackageWidget::emitSearchGroup, [=](const QString group){
        searchEdit->setText(QString("g:%1").arg(group));
        // search()
        // search->search(QString("g:%1").arg(group), searchNumber->text().toInt(), "json");
    });
    connect(packageWidget, &MavenPackageWidget::emitSearchArtifact, [=](const QString artifact){
        searchEdit->setText(QString("a:%1").arg(artifact));
        // search->search(QString("a:%1").arg(artifact), searchNumber->text().toInt(), "json");
    });
    connect(packageWidget, &MavenPackageWidget::emitSearchVersion, [=](QString group, QString artifact, QString version){
        search->searchAllVersion(QString("g:%1 AND a:%2").arg(group).arg(artifact), searchNumber->text().toInt(), "json");
    });

	QGridLayout *mavenSearchLayout = new QGridLayout();
    mavenSearchLayout->addWidget(keyLabel, 0, 0, 1, 1);
    mavenSearchLayout->addWidget(searchEdit, 0, 1, 1, 2);
    mavenSearchLayout->addWidget(searchNumberLabel, 1, 0, 1, 1);
    mavenSearchLayout->addWidget(searchNumber, 1, 1, 1, 1);

    mavenSearchLayout->setColumnStretch(2, 1);

    QGroupBox *mavenSearchBox = new QGroupBox("搜索框");
    mavenSearchBox->setLayout(mavenSearchLayout);

    QVBoxLayout *mavenSearchBoxLayout = new QVBoxLayout();
    mavenSearchBoxLayout->addWidget(mavenSearchBox);
    mavenSearchBoxLayout->addWidget(resultWidget);

    QHBoxLayout *mainLayout = new QHBoxLayout;
    mainLayout->addLayout(mavenSearchBoxLayout);
    mainLayout->addWidget(packageWidget);

    setLayout(mainLayout);

    resize(800,500);
}
MainWindow::~MainWindow()
{
	
}
